/*
RetroLeague - Mobile application for The Retro League Podcast
Copyright (C) 2010-2012 Hugues Johnson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.retroleague.episode;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.Html;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.text.style.StyleSpan;

public class Episode implements Serializable{
	private static final long serialVersionUID=-3481809395101754553L;
	private String title;
	private 	String imageUrl;
	private 	String downloadUrl;
	private String description;
	private String episodeSummary;
	private String linksHtml;
	private Spanned descriptionSpan;
	private String pubDate;
	private int enclosureLength;
	private static String linkPatternString="<[a|A].*?<\\/[a|A]>";
	private static Pattern linkPattern;
	
	public Episode(){
		if(linkPattern==null){
	        linkPattern=Pattern.compile(linkPatternString);
		}
	}
	
	public String getLinksHtml(){
		if(this.linksHtml==null){
			String description=this.getDescription();
			if((description!=null)&&(description.length()>0)){
				Matcher matcher=linkPattern.matcher(description);
				StringBuffer sb=new StringBuffer();
		        while(matcher.find()){
		        	sb.append(description.substring(matcher.start(),matcher.end()));
		        	sb.append("<br>");
		        }
				this.linksHtml=sb.toString();
			}
		}
		return(this.linksHtml);
	}
	
	public String getEpisodeSummary(){
		if(this.episodeSummary==null){
			Spanned spanned=this.getDescriptionSpan();
			int end=spanned.nextSpanTransition(0,spanned.length(),StyleSpan.class);
			String summary=spanned.subSequence(1,end).toString();
			summary=summary.replaceAll("\\<.*?>","").replaceAll("\n","");
			this.episodeSummary=summary;
		}
		return(this.episodeSummary);
	}
	
	private synchronized Spanned getDescriptionSpan(){
		if(this.descriptionSpan==null){
			this.descriptionSpan=Html.fromHtml(this.description);
		}
		return(this.descriptionSpan);
	}
	
	public String getTitle(){
		return title;
	}

	public void setTitle(String title){
		this.title=title;
	}
	
	public void setPubDate(String pubDate){
		this.pubDate=pubDate;
	}
	
	public String getImageUrl(){
		if(this.imageUrl==null){
			Spanned spannedText=Html.fromHtml(this.description);
			ImageSpan[] spans=spannedText.getSpans(0,spannedText.length(),ImageSpan.class);			
			if(spans.length>0){
				//assumes the first image is the episode logo
				this.imageUrl=spans[0].getSource();
			}
		}
		return(this.imageUrl);
	}
	
	public void setImageUrl(String imageUrl){
		this.imageUrl=imageUrl;
	}
	
	public String getDownloadUrl(){
		return downloadUrl;
	}
	
	public void setDownloadUrl(String downloadUrl){
		this.downloadUrl=downloadUrl;
	}

	public String getDescription(){
		return description;
	}
	
	public String getPubDate(){
		return pubDate;
	}

	public void setDescription(String description){
		this.description=description;
	}

	public int getEnclosureLength(){
		return enclosureLength;
	}

	public void setEnclosureLength(int enclosureLength){
		this.enclosureLength=enclosureLength;
	}
}